package com.efast.dmom.mgmt.controller.les.pda;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.dmom.mgmt.entity.les.TwDocOrderDetails;
import com.efast.dmom.mgmt.entity.les.TwDocOrderHeader;
import com.efast.dmom.mgmt.entity.les.TwInvSkuLoctl;
import com.efast.dmom.mgmt.entity.les.TwOutbTaskDetail;
import com.efast.dmom.mgmt.service.les.CommonService;
import com.efast.dmom.mgmt.service.les.TwDocOrderDetailsService;
import com.efast.dmom.mgmt.service.les.TwDocOrderHeaderService;
import com.efast.dmom.mgmt.service.les.TwOutbTaskDetailService;
import com.efast.dmom.mgmt.utils.Assist;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * pda 明细发运
 * 
 * 设计到的表： a) 出库订单表TW_DOC_ORDER_HEADER、TW_DOC_ORDER_DETAILS b) 分配表 TW_ACT_ALLOC_LOT_D c) 拣货任务表
 * TW_OUTB_TASK_HEADER、TW_OUTB_TASK_DETAIL d) 库存表TW_INV_SKU_LOC_T_L
 * 
 * @author chlp
 */
@Controller
@RequestMapping(value = "/pdaTwDocOrderDetails")
@Api(description = "pda-->出库-->明细发运")
public class PdaTwDocOrderDetailsController {

	/**
	 * 出库订单表 TW_DOC_ORDER_HEADER
	 */
	private final TwDocOrderHeaderService twDocOrderHeaderService;
	/**
	 * 出库订单表 TW_DOC_ORDER_DETAILS
	 */
	private final TwDocOrderDetailsService twDocOrderDetailsService;
	/**
	 * 拣货任务表 TW_OUTB_TASK_HEADER
	 */
	private final TwOutbTaskDetailService twOutbTaskDetailService;
	private final CommonService commonService;

	public PdaTwDocOrderDetailsController(TwDocOrderHeaderService twDocOrderHeaderService,
			TwDocOrderDetailsService twDocOrderDetailsService, CommonService commonService,
			TwOutbTaskDetailService twOutbTaskDetailService) {
		this.twDocOrderHeaderService = twDocOrderHeaderService;
		this.twDocOrderDetailsService = twDocOrderDetailsService;
		this.commonService = commonService;
		this.twOutbTaskDetailService = twOutbTaskDetailService;
	}

	/**
	 * 传递的参数：so_sheet_no 或者 cust_sheet_id（客户订单号） 返回的结果目标仓库，库区，库位的值是放的是拣货仓库，库位，库位的值。
	 * 拣货detail表和出库单detail表是一一对应的关系，这个是出库单按单分配的时候生成的记录。
	 *
	 * @param twDocOrderDetails
	 *            twDocOrderDetails
	 */
	@ApiOperation(value = "/查询数据")
	@RequestMapping(value = "/queryData", method = RequestMethod.POST)
	public @ResponseBody ResultBean update(@RequestBody TwDocOrderDetails twDocOrderDetails) {
		ResultBean result = new ResultBean(true, "'");
		Assist assist = new Assist();
		// SO单号
		if (twDocOrderDetails.getSoSheetNo() != null) {
			assist.setRequires(Assist.andEq("so_sheet_no", twDocOrderDetails.getSoSheetNo()));
		}
		// 客户订单号
		if (twDocOrderDetails.getCustSheetId() != null) {
			assist.setRequires(Assist.andEq("cust_sheet_id", twDocOrderDetails.getCustSheetId()));
		}
		List<TwDocOrderDetails> twDocOrderDetails1 = twDocOrderDetailsService.selectTwDocOrderDetails(assist);
		List<TwDocOrderDetails> resultTwDocOrderDetails = new ArrayList<>();
		for (TwDocOrderDetails twDocOrderDetails2 : twDocOrderDetails1) {
			List<TwOutbTaskDetail> twOutbTaskDetails = twOutbTaskDetailService.selectTwOutbTaskDetail(
					new Assist(Assist.andEq("so_sheet_no", twDocOrderDetails2.getSoSheetNo()),
							Assist.andEq("so_lineno", twDocOrderDetails2.getSoLineno())));
			if (twOutbTaskDetails.size() == 0) {
				throw new ServiceException("出库单号无法查询出对应的拣货任务");
			}
			// 通过库位id，查询出对应的仓库编号，库区编号，库位编号
			TwInvSkuLoctl twInvSkuLoctl = commonService.queryWhDlocLoc(twOutbTaskDetails.get(0).getActTmBasStorageId());
			// 把拣货仓库，库区，库位的值放在目标仓库，库区，库位上面。仅供前端显示
			twDocOrderDetails2.setAfterPartWarehouseNo(twInvSkuLoctl.getWarehouseNo());
			twDocOrderDetails2.setAfterPartDlocNo(twInvSkuLoctl.getDlocNo());
			twDocOrderDetails2.setAfterPartLocNo(twInvSkuLoctl.getLocNo());
			resultTwDocOrderDetails.add(twDocOrderDetails2);
		}
		result.setObject(resultTwDocOrderDetails);
		return result;
	}

	/**
	 * 【确认发运】--Shipment
	 * 
	 * @param map
	 *            {"pdaTwDocOrderDetailsBean":[{}]}
	 */
	@RequestMapping(value = "/shipment", method = RequestMethod.POST)
	public @ResponseBody ResultBean shipment(@RequestBody Map<String, Object> map) {
		ResultBean result = new ResultBean(true, "'");
		List<TwDocOrderDetails> listTwDocOrderDetails = (List<TwDocOrderDetails>) map.get("pdaTwDocOrderDetailsBean");
		List<TwDocOrderDetails> list = new ArrayList<>();
		for (TwDocOrderDetails listTwDocOrderDetail : listTwDocOrderDetails) {
			TwDocOrderDetails twDocOrderDetails = JSON.parseObject(JSON.toJSONString(listTwDocOrderDetail),
					TwDocOrderDetails.class);
			list.add(twDocOrderDetails);
		}
		if (list.size() == 0) {
			throw new ServiceException("发运明细数据为空");
		}
		TwDocOrderHeader twDocOrderHeader = twDocOrderHeaderService.selectTwDocOrderHeaderById(
				list.get(0).getTwDocOrderHeaderId());
		twDocOrderHeaderService.shipment(twDocOrderHeader, list);
		result.setMessage("发运成功");
		return result;
	}

}
